package duyd.jpa.training.main;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

import org.springframework.context.support.GenericXmlApplicationContext;

import duyd.jpa.training.domain.Contact;
import duyd.jpa.training.domain.ContactTelDetail;
import duyd.jpa.training.domain.Hobby;
import duyd.jpa.training.domain.MyEntity;
import duyd.jpa.training.service.MyEntityService;

public class JpaExample {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
		ctx.load("application-context.xml");
		ctx.refresh();

		MyEntityService myEntityService = ctx.getBean("myEntityService",
				MyEntityService.class);
		List<MyEntity> myEntityList = new ArrayList<MyEntity>();
		for (int i = 0; i < 10; i++) {
			MyEntity myEntity = new MyEntity("Entity " + i, new Date(),
					new Date(), 'N');
			for (int j = 0; j < 10; j++) {
				if (i == j) {
					continue;
				}
				MyEntity entityOne = new MyEntity("Entity " + j, new Date(),
						new Date(), 'N');
				myEntity.getEntitySetOne().add(entityOne);
			}
			myEntityList.add(myEntity);
		}

		myEntityService.batchSave(myEntityList, 50);

		myEntityList = myEntityService.findAllWithDetails();
		listMyEntityWithDetails(myEntityList);

		// ContactService contactService = ctx.getBean("jpaContactService",
		// ContactService.class);
		// List<Contact> contacts = contactService.findAllWithDetail();
		// listContactsWithDetails(contacts);
		//
		// Contact contact = contactService.findById(1L);
		// contact.setBirthDate(new Date());
		// Set<ContactTelDetail> tels = contact.getTels();
		// ContactTelDetail telToDelete = null;
		// for (ContactTelDetail tel : tels) {
		// if (tel.getType().equals("Home")) {
		// telToDelete = tel;
		// }
		// }
		// tels.remove(telToDelete);
		// contactService.save(contact);
		// contacts = contactService.findAllWithDetail();
		// listContactsWithDetails(contacts);
		//
		// contact = contactService.findById(1L);
		// contactService.delete(contact);
		// contacts = contactService.findAllWithDetail();
		// listContactsWithDetails(contacts);
		//
		// contacts = contactService.findByFirstNameAndLastNameCriteriaQuery(
		// "John", "Smith");
		// listContactsWithDetails(contacts);
		//
		// try {
		// contacts = contactService
		// .findByBirthDateNativeQuery(new SimpleDateFormat(
		// "yyyy-MM-dd").parse("1990-11-02"));
		// listContacts(contacts);
		// } catch (ParseException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
	}

	public static void listContactsWithDetails(List<Contact> contacts) {
		for (Contact contact : contacts) {
			System.out.println(contact);
			Set<ContactTelDetail> tels = contact.getTels();
			for (ContactTelDetail tel : tels) {
				System.out.println("\t" + tel);
			}
			Set<Hobby> hobbies = contact.getHobbies();
			for (Hobby hobby : hobbies) {
				System.out.println("\t" + hobby);
			}
		}
	}

	public static void listContacts(List<Contact> contacts) {
		for (Contact contact : contacts) {
			System.out.println(contact);
		}
	}

	public static void listMyEntityWithDetails(List<MyEntity> myEntityList) {
		for (MyEntity myEntity : myEntityList) {
			System.out.println(myEntity);
			System.out.println("== Entity Set One: ");
			for (MyEntity entityOne : myEntity.getEntitySetOne()) {
				System.out.println(entityOne);
			}
			System.out.println("== Entity Set Two: ");
			for (MyEntity entityTwo : myEntity.getEntitySetTwo()) {
				System.out.println(entityTwo);
			}
		}
	}

}
